#!/bin/bash

TOKEN=`cat /proc/sys/kernel/random/uuid |sed 's/-//g'`

function SETUP_LABEL_STUDIO()
{
  local input_dir=$1;
  local label_config=$2;
  local prefix=$3;
  local task=$4;
  local namespace=$5;

  # init label-studio
  LABEL_STUDIO_PREFIX=$prefix LABEL_STUDIO_LOCAL_FILES_SERVING_ENABLED=true LABEL_STUDIO_LOCAL_FILE_DOCUMENT_ROOT=$input_dir label-studio init DatasetLabel --username admin@huawei.com --password admin@huawei.com --user-token ${TOKEN} --label-config $label_config -p 8008 --data-dir /outputs/label-studio;
  LABEL_STUDIO_PREFIX=$prefix LABEL_STUDIO_LOCAL_FILES_SERVING_ENABLED=true LABEL_STUDIO_LOCAL_FILE_DOCUMENT_ROOT=$input_dir label-studio start DatasetLabel --label-config $label_config -p 8008 --data-dir /outputs/label-studio &

  # get label-studio token
  TOKEN=`label_studio user --username admin@huawei.com --data-dir /outputs/label-studio | sed 's/,/\n/g' | grep "token" | sed 's/:/\n/g' | sed '1d' | sed $'s/\'//g'`
  if [ ! -f "/outputs/import_finish_flag" ];then
    # upload dataset to label-studio workplace
    post_file=`awk '{print "-F FileUpload" NR "=@/inputs/" $0 }'  /outputs/data_list.txt`;
    while true; do
        curl -H "AUTHORIZATION: Token ${TOKEN}" -X POST ${post_file} "http://localhost:8008/api/projects/1/import";
        if [ $? == 0 ]; then
            break
        fi
        sleep 2;
    done

    if [ ! -f "/outputs/prelabel.json" ];then
      touch /outputs/import_finish_flag
      return 0;
    fi
    python /var/run/convert_json_file.py --task_name "$namespace/$task";

    curl -H "AUTHORIZATION: Token ${TOKEN}" -X POST "http://localhost:8008/api/dm/actions?id=delete_tasks&tabID=1&project=1";
    curl -H "AUTHORIZATION: Token ${TOKEN}" -X POST -F "FileUpload=@/tmp/convert.json" "http://localhost:8008/api/projects/1/import";
    if [ $? != 0 ]; then
      curl -H "AUTHORIZATION: Token ${TOKEN}" -X POST -F "FileUpload=@/tmp/convert.json" "http://localhost:8008/api/projects/1/import";
    fi
    touch /outputs/import_finish_flag
  fi
  return 0

}

while getopts 'i:l:t:n:a:s:' opt; do
  case $opt in
    i)
      input_dir=$OPTARG;;
    l)
      label_config=$OPTARG;;
    t)
      prefix=$OPTARG;;
    n)
      task=$OPTARG;;
    a)
      auto_label=$OPTARG;;
    s)
      namespace=$OPTARG;;
    ?)
      echo "Usage: `basename $0` [options] filepath"
    esac
done

SETUP_LABEL_STUDIO "$input_dir" "$label_config" "$prefix" "$task" "$namespace"

if [[ "$?" != '0' ]];then
  echo "run failed."
  SETUP_LABEL_STUDIO "$input_dir" "$label_config" "$prefix" "$task" "$namespace"
fi

if [[ "$auto_label" != '0' ]];then
  cd /var/run/label-studio-ml-backend/;
  label-studio-ml init coco-detector --from label_studio_ml/examples/yolov4tiny_detection/yolov4tiny_detection.py
  label-studio-ml start coco-detector &
  sleep 10;
  curl -X POST -H 'Content-type: application/json' http://localhost:8008/api/ml -H "AUTHORIZATION: Token ${TOKEN}" --data '{"url": "http://localhost:9090", "project": 1}';

  task_count=`curl -H "AUTHORIZATION: Token ${TOKEN}" -X GET "http://localhost:8008/api/dm/project" | python -c "import sys, json; print(json.load(sys.stdin)['task_count'])"`
  for i in $(seq 1 $task_count)
  do
    curl -H "AUTHORIZATION: Token ${TOKEN}" -X GET "http://localhost:8008/api/dm/tasks/${i}?project=1";
  done
fi

while true; do
  curl -H "AUTHORIZATION: Token ${TOKEN}" -X GET "http://localhost:8008/api/projects/1/export?exportType=JSON" --output "/outputs/annotation.json";
  sleep 5
done